package com.lishe.lishe.lishe.MyRecyclerView;

import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by shino on 2017/9/28.
 */

public class RecyclerHerizontalLinearItemDecoration extends RecyclerView.ItemDecoration {
    int rightSpace;//间隔
    int leftSpace;//

    public RecyclerHerizontalLinearItemDecoration(int leftSpace, int rightSpace) {
        this.leftSpace = leftSpace;
        this.rightSpace = rightSpace;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        int count=parent.getAdapter().getItemCount();
        LinearLayoutManager manager = (LinearLayoutManager) parent.getLayoutManager();
        if (manager instanceof LinearLayoutManager) {
            if (manager.getOrientation() == LinearLayout.HORIZONTAL) {
                if(count!=1) {
                    if (position == 0) {
                        outRect.left = leftSpace;
                        outRect.right = rightSpace;
                    } else {
                        if (position != count - 1)
                            outRect.right = rightSpace;
                    }
                }
            }
        }
    }
}
